package com.c.ctgproject.wiget.dialog

import android.app.Dialog
import android.content.Context

import android.view.Gravity
import android.view.WindowManager

import android.widget.NumberPicker
import android.widget.TextView

import com.c.ctgproject.R


class StringChooseDialog:Dialog {
    var mGroupPicker: NumberPicker? = null
    var mConfirmTV:TextView?=null
    var mCancelTV:TextView?=null


    constructor(context: Context, themeStyle: Int) : super(context, themeStyle) {
        initView()
    }
    private fun initView(){
        setContentView(R.layout.choose_string_dialog)
        mGroupPicker=findViewById(R.id.mGroupPicker)
        mConfirmTV=findViewById(R.id.mConfirmTV)
        mCancelTV=findViewById(R.id.mCancelTV)
    }



    class Builder(val context: Context) {
        var selectListener:CallBack?=null
        var groupList=ArrayList<String>()
        var selectPosition=0
        var text=""
        fun setSelectListener(listener: CallBack): Builder {
            this.selectListener = listener
            return this
        }
        fun initString(text:String): Builder{
            this.text= String()
            return this
        }
        fun setList(list:List<String>):Builder{
            this.groupList.addAll(list)
            return this
        }
        fun create(): StringChooseDialog {
            val dialog = StringChooseDialog(context, R.style.CommonDialog)
            dialog.window?.setGravity(Gravity.BOTTOM)
            //设置不遮挡后面
            //dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            //获得window窗口的属性
            val lp = dialog.window?.attributes
            //设置窗口宽度为充满全屏
            //dialog.dialog_phone?.text = phonetext
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.WRAP_CONTENT


            dialog.mCancelTV?.setOnClickListener {
                dialog.dismiss()
            }

//            MyLog.d("${values.toTypedArray().size}")

            dialog.mGroupPicker?.displayedValues= groupList.toTypedArray()
            dialog.mGroupPicker?.minValue= 0
            dialog.mGroupPicker?.maxValue= groupList.size-1
           
            dialog.mGroupPicker?.setOnValueChangedListener { picker, oldVal, newVal ->
                selectPosition=newVal
            }
            dialog?.mConfirmTV?.setOnClickListener {
                selectListener?.selectedLabel(selectPosition,groupList[selectPosition])
                dialog.dismiss()
            }
            //控件与onclick绑定
//            if (cancelListener != null) {
//                dialog.tvDialogCancel?.setOnClickListener { v -> cancelListener!!.onClick(dialog) }
//            }
//            if (confirmListener != null) {
//                dialog.btDialogConfirm?.setOnClickListener { v -> confirmListener!!.onClick(dialog) }
//            }
            return dialog
        }
    }
    interface CallBack{
        fun selectedLabel(position:Int,item:String)
    }
}